<template>
	<view class="login-container">
		<uni-icons type='contact-filled' size="100" color="#afafaf"></uni-icons>
		<button class="btn-login" type="primary" @click="getUserProfile">一键登录</button>
		<text class="tips-text">登陆后享更多权益</text>
	</view>
</template>

<script>
	import {
		mapState,
		mapMutations
	} from 'vuex'
	export default {
		name: "my-login",
		data() {
			return {

			};
		},
		computed:{
			...mapState('m_user',['redirectInfo'])
		},
		methods: {
			...mapMutations('m_user', ['getUserinfo', 'getToken','updateRedirectInfo']),
			getUserProfile() {
				uni.getUserProfile({
					desc: "登录",
					success: (res) => {
						// console.log(res)
						this.getUserinfo(res.userInfo);
						uni.showToast({
							icon: 'success',
							title: '登陆成功'
						})
						this.getTokens(res)
					},
					fail: (e) => {
						uni.showToast({
							icon: "error",
							title: '登陆失败'
						})
					}
				})
			},
			getTokens(e) {
				uni.login({
					provider: 'weixin',
					success: (res) => {
						const query = {
							code: res.code,
							encryptedData: e.encryptedData,
							iv: e.iv,
							rawData: e.rawData,
							signature: e.signature
						}
						// uni.request({
						// 	url:'https://api-hmugo-web.itheima.net/api/public/v1/users/wxlogin',
						// 	method:'POST',
						// 	data:query,
						// 	success: (response) => {
						// 		console.log(response)
						// 	},
						// 	fail: (err) => {
						// 		// console.log(err)
						// 	}
						// })
						this.getToken(
							'Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1aWQiOjIzLCJpYXQiOjE1NjQ3MzAwNzksImV4cCI6MTAwMTU2NDczMDA3OH0.YPt-XeLnjV-_1ITaXGY2FhxmCe4NvXuRnRB8OMCfnPo'
						)
						this.navigateBack()
					}
				})
			},
			navigateBack(){
				if(this.redirectInfo && this.redirectInfo.openType==='switchTab'){
					uni.switchTab({
						url:this.redirectInfo.from,
						complete: () => {
							this.updateRedirectInfo(null)
						}
					})
				}
			}
		}
	}
</script>

<style lang="scss">
	.login-container {
		width: 100vw;
		height: 60vh;
		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: center;
		background-color: #f8f8f8;

		.btn-login {
			width: 80%;
			border-radius: 45rpx;
			background-color: #c00000;
		}

		.tips-text {
			font-size: 24rpx;
			color: gray;
			margin: 20rpx 0;
		}
	}
</style>
